$(document).ready(function() {
    $("#datepicker").kendoDatePicker({
        value: new Date(),
        max: new Date()
    });
    $('input[data-role="datepicker"]').attr('readonly', 'readonly');
    $('label.answer input[type=radio]').click(renderQualification);

    $('#save').click(function() {
        var checkedAnswers = $('label.answer input[type=radio]:checked').length;
        if (appBosch.numQuestions === checkedAnswers) {
            wndAlertSupervisor();
        }
        else {
            $('tr.question').addClass('alert alert-danger');
            $('tr input[type=radio]:checked').forEach(function (e) {
                console.log(e);
            });
            alert('Es necesario calificar todas las preguntas');
        }
        return false;
    });
    $('label.answer input[type=radio]').click(function (e) {
        $(e.currentTarget).parents('tr').removeClass('alert alert-danger');
    });
    $('#allAnswers').dblclick(function() {
        $('label.answer input[value="1"]').attr('checked', 'checked');
        renderQualification();
    });
});

function renderQualification() {
    $(this).parents('tr').removeClass('danger');
    var oks = $('form label.answer input:checked[value="1"]').length;
    var qualification = (oks / appBosch.numQuestions * 100).toFixed(2);
    var classQualification = 'text-danger';
    $('form i.fa-check').addClass('hidden');
    if (qualification >= appBosch.minQualification) {
        classQualification = 'text-success';
        $('form i.fa-check').removeClass('hidden');
    }
    $('#qualification').text(qualification).removeClass('text-danger').removeClass('text-success').addClass(classQualification);
}

function wndAlertSupervisor() {
    var window = $("#wndAlertSupervisor");
    window.removeClass('hidden');
    if (!window.data("kendoWindow")) {
        window.kendoWindow({
            width: 500,
            actions: [
                "Close"
            ],
            open: function () {
                this.center();
            }
        });
    }
}